﻿#include "frmabout.h"
#include "ui_frmabout.h"
#include "quiwidget.h"
#include "panelwidget.h"

FrmAbout::FrmAbout(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FrmAbout)
{
    ui->setupUi(this);
    this->initForm();
}

FrmAbout::~FrmAbout()
{
    delete ui;
}

void FrmAbout::initForm()
{
    //引入图形字体
    int fontId = QFontDatabase::addApplicationFont(":/images/fontawesome-webfont.ttf");
    QString fontName = QFontDatabase::applicationFontFamilies(fontId).at(0);
    iconFont = QFont(fontName);

    initCommon();
    initTableWidget();
    initVideo();
    initFontPanel();
}

void FrmAbout::initTableWidget()
{
    //设置列数和列宽
    int width = qApp->desktop()->availableGeometry().width() - 120;
    ui->tableWidget->setColumnCount(5);
    ui->tableWidget->setColumnWidth(0, width * 0.06);
    ui->tableWidget->setColumnWidth(1, width * 0.10);
    ui->tableWidget->setColumnWidth(2, width * 0.06);
    ui->tableWidget->setColumnWidth(3, width * 0.10);
    ui->tableWidget->setColumnWidth(4, width * 0.15);
    ui->tableWidget->verticalHeader()->setDefaultSectionSize(25);

    QStringList headText;
    headText << "设备编号" << "设备名称" << "设备地址" << "告警内容" << "告警时间";
    ui->tableWidget->setHorizontalHeaderLabels(headText);
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableWidget->setAlternatingRowColors(true);
    ui->tableWidget->verticalHeader()->setVisible(false);
    ui->tableWidget->horizontalHeader()->setStretchLastSection(true);

    //设置行高
    ui->tableWidget->setRowCount(300);

    for (int i = 0; i < 300; i++) {
        ui->tableWidget->setRowHeight(i, 24);

        QTableWidgetItem *itemDeviceID = new QTableWidgetItem(QString::number(i + 1));
        QTableWidgetItem *itemDeviceName = new QTableWidgetItem(QString("测试设备%1").arg(i + 1));
        QTableWidgetItem *itemDeviceAddr = new QTableWidgetItem(QString::number(i + 1));
        QTableWidgetItem *itemContent = new QTableWidgetItem("防区告警");
        QTableWidgetItem *itemTime = new QTableWidgetItem(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));

        ui->tableWidget->setItem(i, 0, itemDeviceID);
        ui->tableWidget->setItem(i, 1, itemDeviceName);
        ui->tableWidget->setItem(i, 2, itemDeviceAddr);
        ui->tableWidget->setItem(i, 3, itemContent);
        ui->tableWidget->setItem(i, 4, itemTime);
    }
}

void FrmAbout::initVideo()
{
    ui->widgetVideo->setStyleSheet("QLabel{font:20pt;}");
    ui->widgetVideo->setProperty("video", true);

    QList<QLabel *> labChs = ui->widgetVideo->findChildren<QLabel *>();
    foreach (QLabel *lab, labChs) {
        lab->setFocusPolicy(Qt::StrongFocus);
    }
}

void FrmAbout::initCommon()
{
    initListWidget();
    ui->rbtn1->setChecked(true);
    ui->ck2->setChecked(true);
    ui->ck3->setCheckState(Qt::PartiallyChecked);
    ui->horizontalSlider->setValue(88);
}

void FrmAbout::initListWidget()
{
    QStringList items;
    for (int i = 1; i <= 30; i++) {
        items << QString("元素%1").arg(i);
    }

    ui->cbox1->addItems(items);
    ui->cbox2->addItems(items);
}

void FrmAbout::initFontPanel()
{
    PanelWidget *widgetFontPanel = new PanelWidget(ui->tab4);
    widgetFontPanel->setObjectName(QString::fromUtf8("widgetFontPanel"));
    ui->gridLayout_6->addWidget(widgetFontPanel, 0, 0, 1, 1);


    //加载图形字体面板
    qDeleteAll(labs);
    labs.clear();
    iconFont.setPixelSize(15);

    QStringList list;
    for (int i = 0; i < 740; i++) {
        QChar str = QChar(0xf000 + i);
        QString tip = QString("0xf%1").arg(i, 3, 16, QChar('0'));
        if (!checkFont(str)) {
            list << tip;
            continue;
        }

        QLabel *lab = new QLabel;
        lab->setAlignment(Qt::AlignCenter);
        lab->setFont(iconFont);
        lab->setText(str);
        lab->setToolTip(tip);
        lab->setMinimumSize(30, 30);
        labs.append(lab);
    }

    qDebug() << "Init Font Panel" << list.count() << list;

    widgetFontPanel->setWidget(labs, 20);
    widgetFontPanel->setMargin(3);
    widgetFontPanel->setSpacing(3);
    //设置图形字体面板鼠标悬停时的效果
    QString qss = QString("QLabel:hover,QLabel:focus{color:%1;border:1px solid %1;}").arg("#00BB9E");
    widgetFontPanel->setStyleSheet(qss);
}

bool FrmAbout::checkFont(QChar str)
{
    bool exist = false;

    //过滤不存在的图形字体
    QPixmap pix = IconHelper::Instance()->getPixmap("#FF0000", str);
    //对该图片逐个扫描像素点,都是空白则意味着当前图形字体不存在
    QImage img = pix.toImage();
    int width = img.width();
    int height = img.height();

    for (int i = 0; i < height; i++) {
        uchar *lineByte = img.scanLine(i);
        for (int j = 0; j < width; j++) {
            uchar tp = lineByte[j];
            if (tp > 0x00) {
                exist = true;
                break;
            }
        }

        if (exist) {
            break;
        }
    }

    return exist;
}

void FrmAbout::on_btnInfo_clicked()
{
     QUIHelper::showMessageBoxInfo("恭喜你,中了500万大奖!请到飞扬青云处领取大奖!");
}

void FrmAbout::on_btnQuestion_clicked()
{
    QUIHelper::showMessageBoxQuestion("确定不要当前的500万大奖吗?");
}

void FrmAbout::on_btnError_clicked()
{
     QUIHelper::showMessageBoxError("领取500万大奖发生错误,请重试!");
}

void FrmAbout::on_btnInput_clicked()
{
    bool ok;
    QString txt = QUIHelper::showInputBox(ok, "请输入姓名:");

    if (ok) {
        QUIHelper::showMessageBoxInfo(QString("您输入的姓名是 : %1").arg(txt));
    }
}

void FrmAbout::on_btnInputPwd_clicked()
{
    bool ok;
    QString txt = QUIHelper::showInputBox(ok, "请输入领取500万大奖密码:", 0, 0, "领取500万大奖密码", true);

    if (ok) {
        QUIHelper::showMessageBoxInfo(QString("您输入的密码是 : %1").arg(txt));
    }
}

void FrmAbout::on_btnInputcbox_clicked()
{
    bool ok;
    QStringList defaultValue;

    for (int i = 1; i <= 50; i++) {
        defaultValue.append(QString::number(i));
    }

    QString txt = QUIHelper::showInputBox(ok, "请选择学号:", 1, 0, defaultValue.join("|"));

    if (ok) {
        QUIHelper::showMessageBoxInfo(QString("您选择的学号是 : %1").arg(txt));
    }
}

void FrmAbout::on_btnWidget_clicked()
{
    //两种方式弹出都可以
#if 0
    QUIWidget *qui = new QUIWidget;
    qui->setTitle("新窗体测试");
    qui->setAlignment(Qt::AlignCenter);
    qui->setMainWidget(new QWidget);
    qui->show();
#else
    QUIWidget qui;
    qui.setTitle("新窗体测试");
    qui.setAlignment(Qt::AlignCenter);
    qui.setMainWidget(new QWidget);
    qui.setOnlyCloseBtn();
    qui.exec();
#endif
}
